#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H

#include "request.h"
#include "response.h"

#include <QJsonDocument>
#include <QNetworkAccessManager>
#include <QNetworkCookie>
#include <QNetworkRequest>
#include <QObject>
#include <QSslConfiguration>


template<typename T>
class Singleton {
public:
    static T& instance(){
        static T t;
        return t;
    }
    Singleton(const T &) = delete;
    Singleton(T &&) = delete;
    Singleton &operator=(const T &) = delete;
    Singleton &operator=(T &&) = delete;

protected:
    Singleton() = default;
    ~Singleton() = default;
};

class HttpClient : public QObject, public Singleton<HttpClient>
{
    Q_OBJECT
public:
    Response head(Request req);
    Response get(Request req);
    Response post(Request req);

    Response head(QString url);
    Response get(QString url);

    void setCookies(QList<QNetworkCookie> cookies);
    void setCookies(QList<QNetworkCookie> &cookies);

    // 设置是否自动处理 Accept 请求头
    void setAutoAccept(bool newAutoAccept);
    // 设置是否自动处理 Host 请求头
    void setAutoHost(bool newAutoHost);
    // 设置是否自动处理 User-Agent 请求头
    void setAutoUserAgent(bool newAutoUserAgent, QString newUserAgent = "Client Qt-HTTP/1.0 (1.0.0)");
    // 设置是否自动处理 301/302
    void setAutoLocation(bool newAutoLocation);

private:
    QNetworkRequest do1(Request &req);
    
    void applyRequestHeaders(Request &req, QNetworkRequest &request);
    QList<QNetworkCookie> m_cookies;

    bool m_auto_location = false;

    bool m_auto_accept = false;
    bool m_auto_host = false;
    bool m_auto_user_agent = false;
    QString m_user_agent;
};

#endif // HTTPCLIENT_H
